package com.pengheng.model;

import java.util.Optional;

/**
 * enum 枚举
 * ResultCode 返回信息类
 * code：编码   txtCode：中文解释
 */
public enum ResultCode {

    /**
     * 系统异常或操作逻辑不正确
     **/
    ERROR(-1, "操作失败"),

    /**
     * 操作成功
     **/
    SUCCESS(0, "操作成功"),

    /**
     * 表单信息验证失败
     **/
    PARAMETER_FAILURE(400, "参数异常"),

    /**
     * 表单信息验证失败
     **/
    SYSTEM_FAULT(2, "系统错误"),

    /**
     * 表单信息验证失败
     **/
    ACCESS_DENIED(403, "无权限访问"),

    /**
     * 请求方法错误
     **/
    REQUEST_METHOD_ERROR(50011, "请求方式错误"),

    /**
     * 请求协议错误
     **/
    REQUEST_PROTOCOL_ERROR(50021, "请求协议错误");


    // 编码
    private Integer code;

    // 中文解释
    private String message;

    ResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public static ResultCode findByName(String name) {
        try {
            return ResultCode.valueOf(name);
        } catch (IllegalArgumentException e) {
            return null;
        }
    }

    public static Integer getReplayCodes(String replyCodeName) {
        return Optional.ofNullable(findByName(replyCodeName)).map(ResultCode::getCode).orElse(-1);
    }

    public static String getReplayTexts(String replyCodeName) {
        return Optional.ofNullable(findByName(replyCodeName)).map(ResultCode::getMessage).orElse("暂未找到传入的字典名称");
    }

//    public static void main(String[] args) {
//
//        //输出某一枚举的值
//        System.out.println(ResultCode.SUCCESS.getCode());
//        System.out.println(ResultCode.SUCCESS.getTxtCode());
//
//        //遍历所有的枚举
//        for (ResultCode replyCode : ResultCode.values()) {
//            System.out.println(replyCode + "  code: " + replyCode.getCode() + "  TxtCode: " + replyCode.getTxtCode());
//        }
//        System.out.println(ResultCode.getReplayCodes("SUCCESS"));
//        System.out.println(ResultCode.getReplayTexts("SUCCESS"));
//    }
}
